package com.bnlr.lrparking.view;

import android.Manifest;
import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.flyco.animation.BounceEnter.BounceTopEnter;
import com.flyco.animation.SlideExit.SlideBottomExit;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.app.AppConfig;

/**
 * Created by hp on 2017/9/18.
 */

public class CarCommon {
    ClickSureListener clickSureListener;
    private NormalDialog dialog;
    private String phone;

    public void setClickSureListener(ClickSureListener clickSureListener) {
        this.clickSureListener = clickSureListener;
    }

    public void phoneDialog(final Context context){
        phone = AppConfig.getInstance().getString("app_phone","4008097095");
        final NormalDialog dialog = new NormalDialog(context);
        dialog.title("拨打电话")
                .titleTextColor(context.getResources().getColor(R.color.black_color))
                .titleTextSize(15)
                .content(phone)
                .contentTextSize(15)
                .contentTextColor(context.getResources().getColor(R.color.gray_color))
                .btnText("取消", "呼叫")
                .btnNum(2)
                .style(NormalDialog.STYLE_TWO)
                .btnTextColor(context.getResources().getColor(R.color.gray_color), context.getResources().getColor(R.color.colorAccent))
                .btnTextSize(15, 15)
                .widthScale(0.8f)//
                .showAnim(new BounceTopEnter())//
                .dismissAnim(new SlideBottomExit())//
                .cornerRadius(7)
                .show();
        dialog.setCancelable(true);
        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            int checkCallPhonePermission = ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE);
                            if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
                                ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CALL_PHONE},100);
                                return;
                            } else {
                                //已有权限
                                Intent intent = new Intent();
                                intent.setAction(Intent.ACTION_CALL);
                                intent.setData(Uri.parse("tel:" +phone));
                                context.startActivity(intent);
                            }
                        } else {
                                //API 版本在23以下
                            Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_CALL);
                            intent.setData(Uri.parse("tel:" +phone));
                            context.startActivity(intent);
                        }
                    }
                });
    }


    public void startShakeByPropertyAnim(View view, float scaleSmall, float scaleLarge, float shakeDegrees, long duration) {
            if (view == null) {
                return;
            }
            //TODO 验证参数的有效性

            //先变小后变大
            PropertyValuesHolder scaleXValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_X,
                    Keyframe.ofFloat(0f, 1.0f),
                    Keyframe.ofFloat(0.25f, scaleSmall),
                    Keyframe.ofFloat(0.5f, scaleLarge),
                    Keyframe.ofFloat(0.75f, scaleLarge),
                    Keyframe.ofFloat(1.0f, 1.0f)
            );
            PropertyValuesHolder scaleYValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_Y,
                    Keyframe.ofFloat(0f, 1.0f),
                    Keyframe.ofFloat(0.25f, scaleSmall),
                    Keyframe.ofFloat(0.5f, scaleLarge),
                    Keyframe.ofFloat(0.75f, scaleLarge),
                    Keyframe.ofFloat(1.0f, 1.0f)
            );

            ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(view, scaleXValuesHolder, scaleYValuesHolder, scaleYValuesHolder);
            objectAnimator.setDuration(duration);
            objectAnimator.start();

    }


/*    public void CommonDialog(Context context, String content){
        dialog = new NormalDialog(context);
        dialog.title("提示")
                .titleTextColor(context.getResources().getColor(R.color.black_color))
                .titleTextSize(15)
                .content(content)
                .contentTextSize(15)
                .contentTextColor(context.getResources().getColor(R.color.gray_color))
                .btnText("取消", "确定")
                .btnNum(2)
                .style(NormalDialog.STYLE_TWO)
                .btnTextColor(context.getResources().getColor(R.color.gray_color), context.getResources().getColor(R.color.colorAccent))
                .btnTextSize(15, 15)
                .widthScale(0.8f)//
                .showAnim(new BounceTopEnter())//
                .dismissAnim(new SlideBottomExit())//
                .cornerRadius(7)
                .show();
        dialog.setCancelable(true);
        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                        clickSureListener.setSure();
                }
                });
    }*/
    public void CommonDialog (Context context, String content,@Nullable String title) {
        View view3 = View.inflate(context,
                R.layout.setting_custom_dialog, null);
        // 对话框
        final Dialog dialog2 = new AlertDialog.Builder(context, R.style.NativeDialog)
                .create();
        dialog2.show();
        dialog2.getWindow().setWindowAnimations(R.style.dialog_anim_style);
        dialog2.getWindow().setContentView(view3);
        TextView tishi2 = (TextView) view3.findViewById(R.id.text_dialog);
        if (title != null) {
            tishi2.setText(title);
        }
        TextView text_phone = (TextView) view3.findViewById(R.id.text_phone);
        text_phone.setText(content);
        // 取消按钮
        TextView btn_Cancel2 = (TextView) view3.findViewById(R.id.cancle);
        btn_Cancel2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog2.dismiss();
            }
        });
        // 确定按钮
        TextView btn_OK2 = (TextView) view3.findViewById(R.id.sure);
        btn_OK2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog2.dismiss();
                clickSureListener.setSure();
            }
        });
    }
    public void finish() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }

    public interface ClickSureListener {
        void setSure();
    }

    /**
     * 单个按钮
     * @param context
     * @param content
     * @param title
     */
    public void ReminderDialog (Context context, String content,@Nullable String title) {
        View view3 = View.inflate(context,
                R.layout.setting_custom_dialog, null);
        // 对话框
        final Dialog dialog2 = new AlertDialog.Builder(context, R.style.NativeDialog)
                .create();
        dialog2.show();
        dialog2.getWindow().setWindowAnimations(R.style.dialog_anim_style);
        dialog2.getWindow().setContentView(view3);
        dialog2.setCancelable(false);
        dialog2.setCanceledOnTouchOutside(false);
        TextView tishi2 = (TextView) view3.findViewById(R.id.text_dialog);
        if (title != null) {
            tishi2.setText(title);
        }
        TextView text_phone = (TextView) view3.findViewById(R.id.text_phone);
        text_phone.setText(content);
        // 取消按钮
        TextView btn_Cancel2 = (TextView) view3.findViewById(R.id.cancle);
        btn_Cancel2.setVisibility(View.GONE);

        TextView btn_OK2 = (TextView) view3.findViewById(R.id.sure);
        btn_OK2.setText("我知道了");
        btn_OK2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickSureListener != null) {
                    clickSureListener.setSure();
                }
                dialog2.dismiss();
            }
        });
    }
    /**
     * 单个按钮
     * @param context
     * @param content
     * @param title
     */
    public void ReminderDialog (Context context, String content,@Nullable String title,String text) {
        View view3 = View.inflate(context,
                R.layout.setting_custom_dialog, null);
        // 对话框
        final Dialog dialog2 = new AlertDialog.Builder(context, R.style.NativeDialog)
                .create();
        dialog2.show();
        dialog2.getWindow().setWindowAnimations(R.style.dialog_anim_style);
        dialog2.getWindow().setContentView(view3);
        dialog2.setCancelable(false);
        dialog2.setCanceledOnTouchOutside(false);
        TextView tishi2 = (TextView) view3.findViewById(R.id.text_dialog);
        if (title != null) {
            tishi2.setText(title);
        }
        TextView text_phone = (TextView) view3.findViewById(R.id.text_phone);
        text_phone.setText(content);
        // 取消按钮
        TextView btn_Cancel2 = (TextView) view3.findViewById(R.id.cancle);
        btn_Cancel2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog2.dismiss();
            }
        });
        TextView btn_OK2 = (TextView) view3.findViewById(R.id.sure);
        if (text != null) {
            btn_OK2.setText(text);
        }
        btn_OK2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog2.dismiss();
                clickSureListener.setSure();
            }
        });
    }

}
